﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BLL;
using Utility;
using Model;
using System.Collections.Generic;

public partial class VehicleUI_Violation_ViolationAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddl_DealStatus.SelectedIndex = 0;
            BindDriver();
            if (Request.QueryString["Type"] == null)
            {
                if (Session["FilterFrameNO"] != null)
                {
                    txt_FrameNO.Text = Session["FilterFrameNO"].ToString();
                }
            }
            else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_FrameNO.Enabled = false;
                txt_VehicleID.Enabled = false;
                txt_Price.Enabled = false;
                txt_DealTime.Enabled = false;
                txt_NoticeTime.Enabled = false;
                txt_LateFee.Enabled = false;
                txt_Remark.Enabled = false;
                txt_ViolationAddress.Enabled = false;
                txt_ViolationTime.Enabled = false;
                txt_ViolationType.Enabled = false;
                ddl_DealStatus.Enabled = false;
                ddl_Driver.Enabled = false;

                btn_Sure.Visible = false;
            }
            else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }

    protected void BindDriver()
    {
        IList<Employee> dt = new EmployeeBLL().GetAllEmployee((int)Session["OrganID"]);
        ddl_Driver.DataSource = dt;
        ddl_Driver.DataValueField = "EmployeeID";
        ddl_Driver.DataTextField = "Name";
        ddl_Driver.DataBind();
    }


    protected void LoadService()
    {
        int _id = Convert.ToInt32(Request.QueryString["ID"]);
        Violation cs = new ViolationBLL().GetModel(_id);
        if (cs != null)
        {
            txt_FrameNO.Text = cs.FrameNO;
            txt_VehicleID.Text = cs.VehicleID.ToString();
            txt_Price.Text = cs.FineMoney.ToString();
            txt_DealTime.Text = cs.DealTime==DateTime.Parse("1900-1-1")?"":cs.DealTime.ToString();
            txt_NoticeTime.Text = cs.NoticeTime.ToString();
            txt_LateFee.Text = cs.LateFee.ToString() ;
            txt_ViolationAddress.Text = cs.ViolationAddress;
            txt_ViolationTime.Text = cs.ViolationTime.ToString();
            txt_ViolationType.Text = cs.ViolationAct.ToString();
            txt_Remark.Text = cs.Remark == null ? "" : cs.Remark;
            ddl_Driver.SelectedValue = cs.Driver;
            ddl_DealStatus.SelectedValue= cs.DealStatus.ToString();
        }
    }

    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        if (!ValidateService.IsNumber(txt_Price.Text))
        {
            UtilityService.Alert(this.Page, "罚款金额格式错误!");
            txt_Price.Focus();
            return;
        }


        string _frameNO = txt_FrameNO.Text.Trim();
        bool reExist = new VehicleBLL().Exists(_frameNO);
        if (!reExist)
        {
            UtilityService.Alert(this.Page, "不存在该车辆信息,请重新录入车架号!");
            txt_FrameNO.Focus();
            return;
        }

        if (!ValidateService.IsNumber(txt_Price.Text.Trim()))
        {
            UtilityService.Alert(this.Page, "格式错误!");
            txt_Price.Focus();
            return;
        }

        if (!ValidateService.IsNumber(txt_LateFee.Text.Trim()))
        {
            UtilityService.Alert(this.Page, "格式错误!");
            txt_LateFee.Focus();
            return;
        }

        if (Request.QueryString["Type"] == null)
        {
            Violation cs = new Violation();

            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.FineMoney = decimal.Parse(txt_Price.Text.Trim());
            cs.LateFee = decimal.Parse(txt_LateFee.Text.Trim());
            cs.DealTime = string.IsNullOrEmpty(txt_DealTime.Text) ? DateTime.Parse("1900-1-1") : DateTime.Parse(txt_DealTime.Text.Trim());
            cs.NoticeTime = DateTime.Parse(txt_NoticeTime.Text.Trim());
            cs.ViolationAddress = txt_ViolationAddress.Text.Trim();
            cs.ViolationAct = txt_ViolationType.Text.Trim();
            cs.ViolationTime = string.IsNullOrEmpty(txt_ViolationTime.Text) ? DateTime.Parse("1900-1-1") : DateTime.Parse(txt_ViolationTime.Text.Trim());
            cs.Remark = txt_Remark.Text.Trim();
            cs.Driver = ddl_Driver.SelectedValue.ToString();
            cs.DealStatus = ddl_DealStatus.SelectedValue.ToString();
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

            int re = new ViolationBLL().Add(cs);

            if (re > 0)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "ViolationMgr.aspx");
            }
            else
            {
                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            int _id = Convert.ToInt32(Request.QueryString["ID"]);
            Violation cs = new ViolationBLL().GetModel(_id);

            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.FineMoney = decimal.Parse(txt_Price.Text.Trim());
            cs.LateFee = decimal.Parse(txt_LateFee.Text.Trim());
            cs.DealTime = string.IsNullOrEmpty(txt_DealTime.Text) ? DateTime.Parse("1900-1-1") : DateTime.Parse(txt_DealTime.Text.Trim());
            cs.NoticeTime = DateTime.Parse(txt_NoticeTime.Text.Trim());
            cs.ViolationAddress = txt_ViolationAddress.Text.Trim();
            cs.ViolationAct = txt_ViolationType.Text.Trim();
            cs.ViolationTime = string.IsNullOrEmpty(txt_ViolationTime.Text) ? DateTime.Parse("1900-1-1") : DateTime.Parse(txt_ViolationTime.Text.Trim());
            cs.Remark = txt_Remark.Text.Trim();
            cs.Driver = ddl_Driver.SelectedValue.ToString();
            cs.DealStatus = ddl_DealStatus.SelectedValue.ToString();
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

            bool re = new ViolationBLL().Update(cs);

            if (re)
            {

                UtilityService.AlertAndRedirect(this.Page, "修改成功! ", "ViolationMgr.aspx");
            }
            else
            {

                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }

    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViolationMgr.aspx");
    }
}
